import { defineStore } from "pinia";
import type { userCartgoryState } from "./interface";
import {
  getAddAttrListApi,
  getCartgroy1ListApi,
  getCartgroy2ListApi,
  getCartgroy3ListApi,
} from "@/api/product/cartgroyselect";
import type { AttrItem } from "@/api/product/model/attrModel";

export const useCartgroyStroe = defineStore("cartgroy", {
  state: (): userCartgoryState => {
    return {
      cartgroy1Id: undefined,
      cartgroy2Id: undefined,
      cartgroy3Id: undefined,
      cartgroy1List: [],
      cartgroy2List: [],
      cartgroy3List: [],
      attrAgree: [],
      attrValue: undefined,
      id: undefined,
    };
  },
  actions: {
    async getcartgroy1List() {
      this.cartgroy1List = await getCartgroy1ListApi();
    },
    async getcartgroy2List(cartgroy1Id: number) {
      this.cartgroy2List = await getCartgroy2ListApi(cartgroy1Id);
      this.cartgroy2Id = undefined;
      this.cartgroy3Id = undefined;
      this.cartgroy3List = [];
    },
    async getcartgroy3List(cartgroy2Id: number) {
      this.cartgroy3List = await getCartgroy3ListApi(cartgroy2Id);
      this.cartgroy3Id = undefined;
    },
    async getAddAttrList(baseAttrInfo: AttrItem) {
      await getAddAttrListApi(baseAttrInfo);
    },
  },
});
